/*
 * User: rant
 * Date: 25/02/2007
 *
 *
 */
package laMalinche;


import laMalinche.constraint.ConstraintManager;
import laMalinche.constraint.ConstraintResultHolder;
import laMalinche.constraint.ConstraintRuleListener;
import laMalinche.rete.nodes.ConditionNode;
import laMalinche.rete.nodes.TrigerConditionNode;
import com.google.inject.Inject;

/**
 *
 */
public class BasicConstraintManager implements ConstraintManager {
	private ConstraintRuleListener constraintRuleListener;
	private final ConditionNode nonMandatoryFieldValidationCondition;
	private final ConditionNode mandatoryFieldValidationCondition;
	private ConstraintResultHolder constraintResultHolder;
//	private static BasicConstraintManager instance;

   @Inject
	public void setConstraintResultHolder(final ConstraintResultHolder constraintResultHolder) {
		this.constraintResultHolder = constraintResultHolder;
	}

    public ConstraintResultHolder getConstraintResultHolder() {
		return constraintResultHolder;
	}

	public ConditionNode getNonMandatoryFieldValidationCondition() {
		return nonMandatoryFieldValidationCondition;
	}

	public ConditionNode getMandatoryFieldValidationCondition() {
		return mandatoryFieldValidationCondition;
	}

	private BasicConstraintManager() {
        DevMode.getInstance().getInjector().injectMembers(this);
        nonMandatoryFieldValidationCondition = new TrigerConditionNode();
		mandatoryFieldValidationCondition = new TrigerConditionNode();
	}

	public void updateConstraintStatus() {
		constraintResultHolder.initializeConstraintResult();
		getMandatoryFieldValidationCondition().update();
		if (constraintResultHolder.isResultValid()) {
			getNonMandatoryFieldValidationCondition().update();
		}
		rulesUpdated();
	}

	public void rulesUpdated() {
		constraintRuleListener.constraintViolated(constraintResultHolder.getConstraintResult());
	}

	public void setConstraintRuleListener(ConstraintRuleListener constraintRuleListener) {
		this.constraintRuleListener = constraintRuleListener;
	}

}